package com.sina.interfaces.manage.web.controller.sys;

import com.sina.interfaces.core.exception.server.UpdateException;
import com.sina.interfaces.core.web.BaseController;
import com.sina.interfaces.domain.sys.Permission;
import com.sina.interfaces.manage.service.sys.PermissionManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2017-11-06-9:52
 */
@Controller
@RequestMapping("permissionManage")
public class PermissionManageController extends BaseController {

    @Autowired
    @Qualifier("permissionManageService")
    private PermissionManageService permissionManageService;

    @RequestMapping(value = "getPermissionList", method = RequestMethod.GET)
    public ModelAndView getPermissionList(@Valid Permission permission) {
        LOG.info("permission:{}", permission);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(6);
        Long count[] = new Long[1];
        List<Map<String, Object>> permissionList = permissionManageService.getPermissionList(permission, count);
        result.put("data", permissionList);
        result.put("recordsFiltered", count[0]);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "getPermission", method = RequestMethod.GET)
    public ModelAndView getPermission(String name) {
        LOG.info("name:{}", name);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        Permission permission = permissionManageService.getPermissionByName(name);
        result.put("data", permission);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "deletePermission", method = RequestMethod.GET)
    public ModelAndView deletePermission(String id) {
        LOG.info("id:{}", id);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);

        try {
            permissionManageService.deletePermission(Integer.valueOf(id));
            result.put(CODE, OK);
            result.put(MSG, "删除权限成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "updatePermission", method = RequestMethod.POST)
    public ModelAndView updatePermission(@Valid Permission permission) {

        LOG.info("permission:{}", permission);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            permissionManageService.updatePermission(permission);
            result.put(CODE, OK);
            result.put(MSG, "更新权限成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "savePermission", method = RequestMethod.POST)
    public ModelAndView savePermission(@Valid Permission permission) {

        LOG.info("permission:{}", permission);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            permissionManageService.savePermission(permission);
            result.put(CODE, OK);
            result.put(MSG, "新增权限成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "getPermissionListToHtml", method = RequestMethod.GET)
    private ModelAndView getPermissionListToHtml() {

        ModelAndView mv = new ModelAndView("sys/permissionList");

        return mv;
    }
}
